package com.myhouse.common.utils.myquartz.factory;

import com.myhouse.common.exception.MyException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Map;

/**
 * jobDetail工厂（单例模式）
 * @author 章瑜亮
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JobDetailFactory {
    /**
     * 生成实例
     * @param jobClass jobClass
     * @param jobName jobName
     * @param jobGroup jobGroup
     * @param params params
     * @return jobDetail
     */
    public static JobDetail getInstance(
            Class<? extends Job> jobClass,
            String jobName,
            String jobGroup,
            Map<String,Object> params
    ) {
        if (jobClass == null) {
            throw new MyException("任务为空");
        }
        JobDataMap map = new JobDataMap();
        if (params != null) {
            for (Map.Entry<String,Object> entry: params.entrySet()) {
                map.put(entry.getKey(),entry.getValue());
            }
        }
        return JobBuilder.newJob(jobClass)
                .withIdentity(jobName, jobGroup)
                .usingJobData(map)
                .build();
    }

    /**
     * 生成实例
     * @param jobClass jobClass
     * @param jobKey jobKey
     * @param params params
     * @return jobDetail
     */
    public static JobDetail getInstance(
            Class<? extends Job> jobClass,
            JobKey jobKey,
            Map<String,Object> params
    ) {
        if (jobClass == null) {
            throw new MyException("任务为空");
        }
        JobDataMap map = new JobDataMap();
        if (params != null) {
            for (Map.Entry<String,Object> entry: params.entrySet()) {
                map.put(entry.getKey(),entry.getValue());
            }
        }
        return JobBuilder.newJob(jobClass)
                .withIdentity(jobKey)
                .usingJobData(map)
                .build();
    }
}
